package pers.corvey.studyjava.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import pers.corvey.studyjava.util.EncodingUtils;

@Controller
@RequestMapping("/testFile")
public class FileTestController {
    
    private static final File FILD_DIRECTORY;
    
    static {
        String classPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        FILD_DIRECTORY = new File(classPath + "/static/doc/");
        if (!FILD_DIRECTORY.exists()) {
            FILD_DIRECTORY.mkdirs();
        }
    }
    
    @PostMapping
    @ResponseBody
    public String work(MultipartFile[] files, String name) {
        for (MultipartFile file : files) {
            String fileName = file.getOriginalFilename();
            Pattern pattern = Pattern.compile("\\.\\w+$");
            System.out.println();
            System.out.println(file.getSize());
        }
        for (String str : name.split(",")) {
            System.out.println(str);
        }
        return "true";
    }
    
    public static void work(String src) {
        System.out.println(123);
        String regex = ".(\\w+)$";
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(src);
        if (m.find() == true) {
            for (int i=0; i<m.groupCount(); ++i) {
                System.out.println(m.group(i));
            }
        } else {
            System.out.println("wu");
        }
    }
    
    public static void main(String[] args) {
        work("asdfasdfasdf.asdf.lod");
    }
}
